package com.spring.boot.springBoot.utils.commonly;


import java.util.Map;

/**
 * Created by derek on 8/16/16.
 */
public class WebApiResponse<T> {
    public static final int SUCCESS_CODE = 0;
    public static final int ERROR_CODE = 1;

    private int code;
    private String message;
    private T data;

    public WebApiResponse() {
    }

    public WebApiResponse(int status, Map<String, Object> errorAttributes) {
        this.code = status;
        this.message = (String) errorAttributes.get("error");
//        this.message = (String) errorAttributes.get("message");
    }


    public WebApiResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> WebApiResponse<T> success(T data) {
        WebApiResponse<T> response = new WebApiResponse<T>();
        response.setCode(SUCCESS_CODE);
        response.setData(data);
        return response;
    }

    public static <T> WebApiResponse<T> successWithMessage(T data, String message) {
        WebApiResponse<T> response = new WebApiResponse<T>();
        response.setCode(SUCCESS_CODE);
        response.setData(data);
        response.setMessage(message);
        return response;
    }

    public static <T> WebApiResponse<T> success() {
        WebApiResponse<T> response = new WebApiResponse<T>();
        response.setCode(SUCCESS_CODE);
        return response;
    }

    public static <T> WebApiResponse<T> successWithMessage(String message) {
        WebApiResponse<T> response = new WebApiResponse<T>();
        response.setCode(SUCCESS_CODE);
        response.setMessage(message);
        return response;
    }

    public static <T> WebApiResponse<T> error(String errorMessage) {
        return new WebApiResponse<T>(ERROR_CODE, errorMessage);
    }

    public static <T> WebApiResponse<T> error(int errCode, String errorMessage) {
        return new WebApiResponse<T>(errCode, errorMessage);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
